package b3.model
{
	import b3.net.ServerEvent;
	import b3.net.ServerRequest;
	
	public class MailModel extends Model
	{
		public var id:int;
		
		public var fromId:int;
		public var fromName:String;
		
		public var toId:String;
		
		public var message:String;
		
		public var gifts:Array;
		
		public function MailModel()
		{
			super();
		}
		
		public override function loadbyObject(data:Object):void
		{
			super.loadbyObject(data);
			var giftObject:Object = data["giftArray"];
			if (giftObject != null)
			{
				gifts = new Array();
				for (var str:String in giftObject)
				{
					var obj:Obj = new Obj();
					obj.loadbyObject(giftObject[str]);
					gifts.push(obj);
				}
			}
		}
		
		public function send():void
		{
			var serverRequest:ServerRequest = new ServerRequest(ServerEvent.SEND_MAIL, {mail:this});
			serverRequest.setHandleFunc(handleSend);
			serverRequest.post();
		}
		
		private function handleSend(data:Object):void
		{
			PlayerManager.getInstance().me.packageModel.loadObjects(false, true);
			PlayerManager.getInstance().me.loadMail(true);
			var event:ModelEvent = new ModelEvent(ModelEvent.SEND_MAIL_COMPLETE);
			event.data = data; 
			dispatchEvent(event);
		}
		
		public function recieveGift():void
		{
			var serverRequest:ServerRequest = new ServerRequest(ServerEvent.RECIEVE_GIFT,{id:id,userId:toId});
			serverRequest.setHandleFunc(handleRecieveGift);
			serverRequest.post();
		}
		
		private function handleRecieveGift(data:Object):void
		{
			PlayerManager.getInstance().me.packageModel.loadObjects(false, true);
			PlayerManager.getInstance().me.loadMail(true);
		}
	}
}